package yxy.game.pm2.command.capturestrategicpoints;

import yxy.apple.util.ArraryMesh;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.clearancereward;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.CaptureStrategicPointsMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.capturestrategicpoints.CaptureStrategicPointsHandle;
import yxy.game.pm2.module.capturestrategicpoints.CaptureStrategicPointsModule;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

/**
 * @author wrs
 * @descrition 斩关夺隘通关奖励领取(右侧宝箱)
 * @since 2022-08-17
 */
@MessageType(proto = Command.CaptureStrategicPointsPass, classtype = CaptureStrategicPointsMessage.CaptureStrategicPointsPassRequest.class)
public class CMD_CaptureStrategicPointsPass extends Command<CaptureStrategicPointsMessage.CaptureStrategicPointsPassRequest> {

    @Override
    public void execute(MessageSource messageSource, CaptureStrategicPointsMessage.CaptureStrategicPointsPassRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        CaptureStrategicPointsMessage.CaptureStrategicPointsPassResponse.Builder response = CaptureStrategicPointsMessage.CaptureStrategicPointsPassResponse.newBuilder();
        int boxId = request.getBoxId();

        try {
            CaptureStrategicPointsModule module = CaptureStrategicPointsModule.get(playerCode);
            CaptureStrategicPointsHandle handle = new CaptureStrategicPointsHandle();

            if (module == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            clearancereward data = DataCenter.getData(boxId, clearancereward.class);
            if (data == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            int day = data.getField();
            HashMap<Integer, Boolean> rewardMap = module.getOpenCustomsMap().get(day);
            Boolean isReward = rewardMap.get(boxId);
            if (isReward == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            if (isReward) {
                send(messageSource, response, Result.CaptureStrategicPointsCantRepeatedReward);//领过
                return;
            }

            HashMap<Integer, Integer> checkpointRecord = module.getCheckpointRecord();
            Integer integer = checkpointRecord.get(day);//最大通关数

            if (integer == null || integer < data.getCondition()) {
                send(messageSource, response, Result.CaptureStrategicPointsCantReward);//还不能领
                return;
            }

            //todo 领取奖励
            rewardMap.put(boxId, true);
            module.set();

            int degree = module.getDegree();
            int[] num = ArraryMesh.getNum(data.getRewardnum(), data.getDifficulty(), degree, -1);
            Collection<Currency> curs = Currency.create(data.getRewardtype(), data.getRewardid(), num);
            Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
            new CurrencyHandle().addCurrency(playerCode, CCReason.斩关夺隘, curs);
            response.addAllItem(items);//返回奖励


            ArrayList<CaptureStrategicPointsMessage.OneDayMsgModule.Builder> list = handle.getOneDayMsgModule(module);
            for (CaptureStrategicPointsMessage.OneDayMsgModule.Builder builder : list) {
                if (builder.getTime() == data.getField()) {
                   response.setMsgModules(builder);
                }
            }
            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, CaptureStrategicPointsMessage.CaptureStrategicPointsPassResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
